home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODMisc / FWPoint.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  15.2 KB  |  495 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPoint.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPOINT_H
  13. #include "FWPoint.h"
  14. #endif
  15.  
  16. #ifndef FWRECT_H
  17. #include "FWRect.h"
  18. #endif
  19.  
  20. // ----- Foundation Includes -----
  21.  
  22. #ifndef FWSTREAM_H
  23. #include "FWStream.h"
  24. #endif
  25.  
  26. //========================================================================================
  27. //    RunTime Info
  28. //========================================================================================
  29.  
  30. #ifdef FW_BUILD_MAC
  31. #pragma segment fwodmic_point
  32. #endif
  33.  
  34.  
  35. //========================================================================================
  36. //    struct FW_SPoint
  37. //========================================================================================
  38.  
  39. //----------------------------------------------------------------------------------------
  40. //    operator==
  41. //----------------------------------------------------------------------------------------
  42.  
  43. FW_Boolean operator==(const FW_SPoint& pt1, const FW_SPoint& pt2)
  44. {
  45.     return pt1.x == pt2.x && pt1.y == pt2.y;
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------
  49. //    operator!=
  50. //----------------------------------------------------------------------------------------
  51.  
  52. FW_Boolean operator!=(const FW_SPoint& pt1, const FW_SPoint& pt2)
  53. {
  54.     return pt1.x != pt2.x || pt1.y != pt2.y;
  55. }
  56.  
  57. //----------------------------------------------------------------------------------------
  58. //    operator==
  59. //----------------------------------------------------------------------------------------
  60.  
  61. FW_Boolean operator==(const FW_SPoint& pt1, const ODPoint& pt2)
  62. {
  63.     return FW_FixedToODFixed(pt1.x) == pt2.x && FW_FixedToODFixed(pt1.y) == pt2.y;
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. //    operator!=
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_Boolean operator!=(const FW_SPoint& pt1, const ODPoint& pt2)
  71. {
  72.     return FW_FixedToODFixed(pt1.x) != pt2.x || FW_FixedToODFixed(pt1.y) != pt2.y;
  73. }
  74.  
  75. //========================================================================================
  76. //    class FW_CPoint
  77. //========================================================================================
  78.  
  79. //----------------------------------------------------------------------------------------
  80. //    FW_CPoint::FW_CPoint
  81. //----------------------------------------------------------------------------------------
  82.  
  83. FW_CPoint::FW_CPoint(FW_PlatformPoint plfmPoint)
  84. {
  85. #ifdef FW_BUILD_WIN
  86.     x    =    FW_IntToFixed(plfmPoint.x);
  87.     y    =    FW_IntToFixed(plfmPoint.y);
  88. #endif
  89. #ifdef FW_BUILD_MAC
  90.     x    =    FW_IntToFixed(plfmPoint.h);
  91.     y    =    FW_IntToFixed(plfmPoint.v);
  92. #endif
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. //    FW_CPoint::FW_CPoint
  97. //----------------------------------------------------------------------------------------
  98.  
  99. FW_CPoint::FW_CPoint(const FW_CPoint &other)
  100. {
  101.     x = other.x;
  102.     y = other.y;
  103. }
  104.  
  105. //----------------------------------------------------------------------------------------
  106. //    FW_CPoint::FW_CPoint
  107. //----------------------------------------------------------------------------------------
  108.  
  109. FW_CPoint::FW_CPoint(const FW_SPoint &point)
  110. {
  111.     x = point.x;
  112.     y = point.y;
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. //    FW_CPoint::FW_CPoint
  117. //----------------------------------------------------------------------------------------
  118.  
  119. FW_CPoint::FW_CPoint(const ODPoint &odPoint)
  120. {
  121.     x    =    FW_ODFixedToFixed(odPoint.x);
  122.     y    =    FW_ODFixedToFixed(odPoint.y);
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. //    FW_CPoint::FW_CPoint
  127. //----------------------------------------------------------------------------------------
  128.  
  129. FW_CPoint::FW_CPoint(FW_CReadableStream& stream)
  130. {
  131.     stream >> x >> y;
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. //    FW_CPoint::operator=
  136. //----------------------------------------------------------------------------------------
  137.  
  138. FW_CPoint& FW_CPoint::operator=(const FW_SPoint &point)
  139. {
  140.     x = point.x;
  141.     y = point.y;
  142.     return *this;
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. //    FW_CPoint::operator=
  147. //----------------------------------------------------------------------------------------
  148.  
  149. FW_CPoint& FW_CPoint::operator=(const FW_CPoint &other)
  150. {
  151.     x = other.x;
  152.     y = other.y;
  153.     return *this;
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. //    FW_CPoint::operator=
  158. //----------------------------------------------------------------------------------------
  159.  
  160. FW_CPoint& FW_CPoint::operator=(const ODPoint &odPoint)
  161. {
  162.     x = FW_ODFixedToFixed(odPoint.x);
  163.     y = FW_ODFixedToFixed(odPoint.y);
  164.     return *this;
  165. }
  166.  
  167. //----------------------------------------------------------------------------------------
  168. //    FW_CPoint::operator=
  169. //----------------------------------------------------------------------------------------
  170.  
  171. FW_CPoint& FW_CPoint::operator=(FW_PlatformPoint plfmPoint)
  172. {
  173. #ifdef FW_BUILD_WIN
  174.     x    =    FW_IntToFixed(plfmPoint.x);
  175.     y    =    FW_IntToFixed(plfmPoint.y);
  176. #endif
  177. #ifdef FW_BUILD_MAC
  178.     x    =    FW_IntToFixed(plfmPoint.h);
  179.     y    =    FW_IntToFixed(plfmPoint.v);
  180. #endif
  181.     return *this;
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. //    FW_CPoint::Offset
  186. //----------------------------------------------------------------------------------------
  187.  
  188. void FW_CPoint::Offset(FW_Fixed xx, FW_Fixed yy)
  189. {
  190.     x += xx;
  191.     y += yy;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. //    FW_CPoint::operator+=
  196. //----------------------------------------------------------------------------------------
  197.  
  198. FW_CPoint& FW_CPoint::operator+=(const FW_SPoint &point)
  199. {
  200.     x += point.x;
  201.     y += point.y;
  202.     return *this;
  203. }
  204.  
  205. //----------------------------------------------------------------------------------------
  206. //    FW_CPoint::operator-=
  207. //----------------------------------------------------------------------------------------
  208.  
  209. FW_CPoint& FW_CPoint::operator-=(const FW_SPoint &point)
  210. {
  211.     x -= point.x;
  212.     y -= point.y;
  213.     return *this;
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. //    FW_CPoint::operator+=
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_CPoint& FW_CPoint::operator+=(const ODPoint &odPoint)
  221. {
  222.     x += FW_ODFixedToFixed(odPoint.x);
  223.     y += FW_ODFixedToFixed(odPoint.y);
  224.     return *this;
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_CPoint::operator-=
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_CPoint& FW_CPoint::operator-=(const ODPoint &odPoint)
  232. {
  233.     x -= FW_ODFixedToFixed(odPoint.x);
  234.     y -= FW_ODFixedToFixed(odPoint.y);
  235.     return *this;
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CPoint::operator+
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_CPoint FW_CPoint::operator+(const FW_SPoint &point) const
  243. {
  244.     return FW_CPoint(x + point.x, y + point.y);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. //    FW_CPoint::operator-
  249. //----------------------------------------------------------------------------------------
  250.  
  251. FW_CPoint FW_CPoint::operator-(const FW_SPoint &point) const
  252. {
  253.     return FW_CPoint(x - point.x, y - point.y);
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. //    FW_CPoint::operator+
  258. //----------------------------------------------------------------------------------------
  259.  
  260. FW_CPoint FW_CPoint::operator+(const ODPoint &odPoint) const
  261. {
  262.     return FW_CPoint(x + FW_ODFixedToFixed(odPoint.x), y + FW_ODFixedToFixed(odPoint.y));
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. //    FW_CPoint::operator-
  267. //----------------------------------------------------------------------------------------
  268.  
  269. FW_CPoint FW_CPoint::operator-(const ODPoint &odPoint) const
  270. {
  271.     return FW_CPoint(x - FW_ODFixedToFixed(odPoint.x), y - FW_ODFixedToFixed(odPoint.y));
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    FW_CPoint::operator-
  276. //----------------------------------------------------------------------------------------
  277.  
  278. FW_CPoint FW_CPoint::operator-() const
  279. {
  280.     return FW_CPoint(-x, -y);
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //    FW_CPoint::operator+=
  285. //----------------------------------------------------------------------------------------
  286.  
  287. FW_CPoint& FW_CPoint::operator+=(FW_CPlatformPoint plfmPoint)
  288. {
  289.     x += FW_IntToFixed(plfmPoint.X());
  290.     y += FW_IntToFixed(plfmPoint.Y());
  291.     return *this;
  292. }
  293.  
  294. //----------------------------------------------------------------------------------------
  295. //    FW_CPoint::operator-=
  296. //----------------------------------------------------------------------------------------
  297.  
  298. FW_CPoint& FW_CPoint::operator-=(FW_CPlatformPoint plfmPoint)
  299. {
  300.     x -= FW_IntToFixed(plfmPoint.X());
  301.     y -= FW_IntToFixed(plfmPoint.Y());
  302.     return *this;
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CPoint::operator+
  307. //----------------------------------------------------------------------------------------
  308.  
  309. FW_CPoint FW_CPoint::operator+(FW_CPlatformPoint plfmPoint) const
  310. {
  311.     return FW_CPoint(x + FW_IntToFixed(plfmPoint.X()), y + FW_IntToFixed(plfmPoint.Y()));
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. //    FW_CPoint::operator-
  316. //----------------------------------------------------------------------------------------
  317.  
  318. FW_CPoint FW_CPoint::operator-(FW_CPlatformPoint plfmPoint) const
  319. {
  320.     return FW_CPoint(x - FW_IntToFixed(plfmPoint.X()), y - FW_IntToFixed(plfmPoint.Y()));
  321. }
  322.  
  323. //----------------------------------------------------------------------------------------
  324. //    FW_CPoint::AsPlatformPoint
  325. //----------------------------------------------------------------------------------------
  326.  
  327. FW_CPlatformPoint FW_CPoint::AsPlatformPoint() const
  328. {
  329.     return FW_CPlatformPoint(FW_FixedToInt(x), FW_FixedToInt(y));
  330. }
  331.  
  332. //----------------------------------------------------------------------------------------
  333. //    FW_CPoint::operator[]
  334. //----------------------------------------------------------------------------------------
  335.  
  336. FW_Fixed& FW_CPoint::operator[](FW_XYSelector selector)
  337. {
  338.     return * (FW_Fixed *) (selector == FW_kVertical ? &y : &x);
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. //    FW_CPoint::operator[]
  343. //----------------------------------------------------------------------------------------
  344.  
  345. FW_Fixed FW_CPoint::operator[](FW_XYSelector selector) const
  346. {
  347.     return * (FW_Fixed *) (selector == FW_kVertical ? &y : &x);
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. //    FW_CPoint::Map
  352. //----------------------------------------------------------------------------------------
  353.  
  354. void FW_CPoint::Map(const FW_SRect& srcRect, const FW_SRect& dstRect)
  355. {
  356.     FW_Fixed xDelta = x - srcRect.left;
  357.     FW_Fixed yDelta = y - srcRect.top;
  358.  
  359.     FW_Fixed srcSize = srcRect.right - srcRect.left;
  360.     FW_Fixed dstSize = dstRect.right - dstRect.left;
  361.  
  362.     if (srcSize != FW_kFixed0)
  363.         xDelta *= dstSize / srcSize;
  364.  
  365.     x = dstRect.left + xDelta;
  366.  
  367.     srcSize = srcRect.bottom - srcRect.top;
  368.     dstSize = dstRect.bottom - dstRect.top;
  369.  
  370.     if (srcSize != FW_kFixed0)
  371.         yDelta *= dstSize / srcSize;
  372.  
  373.     y = dstRect.top + yDelta;
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    FW_Transform
  378. //----------------------------------------------------------------------------------------
  379.  
  380. void            FW_Transform(Environment* ev, FW_SPoint& pt, ODTransform* transform)
  381. {
  382. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  383.     * ((ODPoint *) &pt) =
  384. #endif
  385.     transform->TransformPoint(ev, (ODPoint*) &pt);
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. //    FW_InverseTransform
  390. //----------------------------------------------------------------------------------------
  391.  
  392. void            FW_InverseTransform(Environment* ev, FW_SPoint& pt, ODTransform* transform)
  393. {
  394. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  395.     * ((ODPoint *) &pt) =
  396. #endif
  397.     transform->InvertPoint(ev, (ODPoint*) &pt);
  398. }
  399.  
  400. //----------------------------------------------------------------------------------------
  401. //    FW_TransformCopy
  402. //----------------------------------------------------------------------------------------
  403.  
  404. FW_CPoint        FW_TransformCopy(Environment* ev, const FW_SPoint& pt, ODTransform* transform)
  405. {
  406.     FW_CPoint point(pt);
  407.     
  408. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  409.     * ((ODPoint *) & point) = 
  410. #endif
  411.     transform->TransformPoint(ev, point);
  412.  
  413.     return point;
  414. }
  415.  
  416. //----------------------------------------------------------------------------------------
  417. //    FW_CPoint::InverseTransformCopy
  418. //----------------------------------------------------------------------------------------
  419.  
  420. FW_CPoint        FW_InverseTransformCopy(Environment* ev, const FW_SPoint& pt, ODTransform* transform)
  421. {
  422.     FW_CPoint point(pt);
  423.     
  424. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  425.     * ((ODPoint *) & point) = 
  426. #endif
  427.     transform->InvertPoint(ev, point);
  428.  
  429.     return point;
  430. }
  431.  
  432. //----------------------------------------------------------------------------------------
  433. //    FW_TransformPoints
  434. //----------------------------------------------------------------------------------------
  435.  
  436. void FW_TransformPoints(Environment* ev, ODTransform* transform, FW_SPoint* pts, long count)
  437. {
  438.     while (count > 0)
  439.     {
  440. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  441.         ODPoint odpt = 
  442. #endif
  443.             transform->TransformPoint(ev, (ODPoint*) pts);
  444.  
  445. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  446.         pts->x = FW_ODFixedToFixed(odpt.x);
  447.         pts->y = FW_ODFixedToFixed(odpt.y);
  448. #endif
  449.  
  450.         ++ pts;
  451.         -- count;
  452.     }
  453. }
  454.  
  455. //----------------------------------------------------------------------------------------
  456. //    FW_InverseTransformPoints
  457. //----------------------------------------------------------------------------------------
  458.  
  459. void FW_InverseTransformPoints(Environment* ev, ODTransform* transform, FW_SPoint* pts, long count)
  460. {
  461.     while (count > 0)
  462.     {
  463. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  464.         ODPoint odpt = 
  465. #endif
  466.             transform->InvertPoint(ev, (ODPoint*) pts);
  467.  
  468. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  469.         pts->x = FW_ODFixedToFixed(odpt.x);
  470.         pts->y = FW_ODFixedToFixed(odpt.y);
  471. #endif
  472.  
  473.         ++ pts;
  474.         -- count;
  475.     }
  476. }
  477.  
  478. //----------------------------------------------------------------------------------------
  479. //    operator<<
  480. //----------------------------------------------------------------------------------------
  481.  
  482. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_SPoint& point)
  483. {
  484.     return stream << point.x << point.y;
  485. }
  486.  
  487. //----------------------------------------------------------------------------------------
  488. //    operator>>
  489. //----------------------------------------------------------------------------------------
  490.  
  491. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_SPoint& point)
  492. {
  493.     return stream >> point.x >> point.y;
  494. }
  495.